from django.db import models

from BYD_ERP.utils.base_model import BaseModel, BaseModel2


# python ../../manage.py startapp sale_info

# 迁移基础：erp_system, basic_info, goods_info

# python ../../manage.py makemigrations sale_info
# python ../../manage.py migrate sale_info

# python ../../manage.py check


# 销售订单（顾客需求计划）模型类
class SaleOrderModel(BaseModel):
    invoices_date = models.DateTimeField('单据日期')
    number_code = models.CharField('单据编号，不让用户填写', max_length=28)
    discount = models.DecimalField('优惠率，最多精确到小数点后两位', max_digits=5, decimal_places=2, blank=True, null=True)
    discount_amount = models.DecimalField('优惠金额，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True,
                                          default=0)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    last_amount = models.DecimalField('优惠后总金额，最多精确到小数点后两位', max_digits=13, decimal_places=2, blank=True, null=True)
    deposit = models.DecimalField('收取定金，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    number_count = models.DecimalField('销售数量，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    status = models.CharField('销售订单的状态，0：未审核，1：已审核，2：部分发货，3：全部发货，4：收取定金，5：完成销售', max_length=10)

    operator_user = models.ForeignKey('erp_system.UserModel', related_name='operator_sale_list', blank=True, null=True,
                                      on_delete=models.SET_NULL, verbose_name='制单人，不能修改')
    # 增加一个冗余字段
    operator_user_name = models.CharField('制单人的真实姓名', max_length=20, blank=True, null=True)

    #  blank=True, null=True
    sales_man = models.ForeignKey('erp_system.UserModel', related_name='sales_man_order_list', blank=True, null=True,
                                  on_delete=models.SET_NULL, verbose_name='业务代表（经办人），不能修改')
    # 增加一个冗余字段
    sales_man_name = models.CharField('业务代表（经办人）的真实姓名', max_length=20, blank=True, null=True)

    check_user = models.ForeignKey('erp_system.UserModel', related_name='check_user_list', on_delete=models.SET_NULL,
                                   blank=True, null=True, verbose_name='审核人员，不能修改')
    # 增加一个冗余字段
    check_user_name = models.CharField('审核人员的真实姓名', max_length=20, blank=True, null=True)

    account = models.ForeignKey('basic_info.SettlementAccountModel', blank=True, null=True, on_delete=models.SET_NULL,
                                verbose_name='结算账户，审核之后不能改')
    customer = models.ForeignKey('basic_info.CustomerModel', blank=True, null=True, on_delete=models.SET_NULL,
                                 verbose_name='顾客，审核之后不能改')
    customer_name = models.CharField('顾客名称', max_length=30, blank=True, null=True)

    attachment_list = models.CharField('附件的ID列表，字段的值为：1,2,3,4', max_length=20, blank=True, null=True)

    class Meta:
        db_table = 't_sales_order'
        verbose_name = '销售订单（顾客需求计划）表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 销售订单项目模型类
# 优化项目：SaleItemModel优化为SaleOrderItemModel
class SaleOrderItemModel(BaseModel2):
    # 这几个也是冗余字段（本质就是以存储空间换取时间）
    name = models.CharField('货品名称', max_length=20)
    specification = models.CharField('规格/件号', max_length=50, blank=True, null=True)
    model_number = models.CharField('型号', max_length=50, blank=True, null=True)
    brand = models.CharField('品牌', max_length=50, blank=True, null=True)
    partner = models.ForeignKey('goods_info.PartnerModel', related_name='partner_sale_list', on_delete=models.SET_NULL,
                                blank=True, null=True, verbose_name='合作厂家')
    units = models.ForeignKey('goods_info.UnitsModel', on_delete=models.SET_NULL, blank=True, null=True,
                              verbose_name='计量单位')
    units_name = models.CharField('计量单位名称', max_length=50, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    sales_count = models.DecimalField('销售数量，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    sales_price = models.DecimalField('销售价格，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    sales_money = models.DecimalField('销售金额，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)

    sale_order = models.ForeignKey('SaleOrderModel', related_name='item_list', blank=True, null=True,
                                   on_delete=models.CASCADE, verbose_name='销售订单')

    goods = models.ForeignKey('goods_info.GoodsModel', blank=True, null=True, on_delete=models.CASCADE,
                              verbose_name='货品')
    warehouse = models.ForeignKey('basic_info.WarehouseModel', blank=True, null=True, on_delete=models.CASCADE,
                                  verbose_name='仓库')

    class Meta:
        db_table = 't_sales_order_items'
        verbose_name = '销售订单项目表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name + ' ' + self.specification

# 经营部月度生产计划OperationDepartmentMonthlyProductionPlanModel
class MonthlyProductionPlanModel(BaseModel2):

    invoices_date = models.DateTimeField('制单日期')
    number_code = models.CharField('单据编号，不让用户填写', max_length=28)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    status = models.CharField('月度生产计划的状态，0：未审核，1：已审核', max_length=10)
    operator_user = models.ForeignKey('erp_system.UserModel', related_name='operator_user_production_plan_list', blank=True, null=True,
                                      on_delete=models.SET_NULL, verbose_name='制单人，不能修改')
    # 增加一个冗余字段
    operator_user_name = models.CharField('制单人的真实姓名', max_length=20, blank=True, null=True)

    check_user = models.ForeignKey('erp_system.UserModel', related_name='check_user_production_plan_list', on_delete=models.SET_NULL,
                                   blank=True, null=True, verbose_name='审核人员，不能修改')
    # 增加一个冗余字段
    check_user_name = models.CharField('审核人员的真实姓名', max_length=20, blank=True, null=True)

    class Meta:
        db_table = 't_monthly_production_plan'
        verbose_name = '月度生产计划表'
        verbose_name_plural = verbose_name
        ordering = ['id']

# 经营部月度生产计划项目表
class MonthlyProductionPlanItemModel(BaseModel2):
    # 这几个也是冗余字段（本质就是以存储空间换取时间）
    customer = models.CharField('需求客户', max_length=20, blank=True, null=True)  # 潍柴/玉柴/市场/佳木斯
    name = models.CharField('货品名称', max_length=20)
    specification = models.CharField('规格/件号', max_length=50, blank=True, null=True)
    model_number = models.CharField('型号', max_length=50, blank=True, null=True)
    partner = models.ForeignKey('goods_info.PartnerModel', related_name='partner_production_plan_list', on_delete=models.SET_NULL,
                                blank=True, null=True, verbose_name='合作厂家')
    partner_name = models.CharField('合作厂家名称', max_length=20, blank=True, null=True)
    units = models.ForeignKey('goods_info.UnitsModel', on_delete=models.SET_NULL, blank=True, null=True,
                              verbose_name='计量单位')
    units_name = models.CharField('计量单位名称', max_length=20, blank=True, null=True)

    local_inventory = models.DecimalField('永驰库存/本地库存，精确到整数', max_digits=10, decimal_places=0, blank=True, null=True)
    customer_inventory = models.DecimalField('客户库存，精确到整数', max_digits=10, decimal_places=0, blank=True, null=True)
    customer_monthly_plan_count = models.DecimalField('客户月度计划需求，精确到整数', max_digits=10, decimal_places=0, blank=True, null=True)
    this_month_planed_count = models.DecimalField('本月累计已下计划，精确到整数', max_digits=10, decimal_places=0, blank=True, null=True)
    monthly_production_plan_count = models.DecimalField('经营部月度新增生产计划（条数），精确到整数', max_digits=10, decimal_places=0, blank=True, null=True)
    emergency_degree = models.CharField('紧急程度', max_length=50, blank=True, null=True)
    leave_factory_date = models.DateTimeField('发货日期/出厂日期', blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    monthly_production_plan = models.ForeignKey('MonthlyProductionPlanModel', related_name='item_list', blank=True, null=True,
                                                on_delete=models.CASCADE, verbose_name='月度生产计划')

    goods = models.ForeignKey('goods_info.GoodsModel', blank=True, null=True, on_delete=models.CASCADE,
                              verbose_name='货品')

    warehouse = models.ForeignKey('basic_info.WarehouseModel', blank=True, null=True, on_delete=models.CASCADE,
                                  verbose_name='仓库')

    sales_man = models.ForeignKey('erp_system.UserModel', related_name='sales_man_production_plan_list', blank=True, null=True,
                                  on_delete=models.SET_NULL, verbose_name='业务员，不能修改')
    # 增加一个冗余字段
    sales_man_name = models.CharField('业务员的真实姓名', max_length=20, blank=True, null=True)

    district_manager = models.ForeignKey('erp_system.UserModel', related_name='district_manager_production_plan_list', blank=True, null=True,
                                         on_delete=models.SET_NULL, verbose_name='片区负责人，不能修改')
    # 增加一个冗余字段
    district_manager_name = models.CharField('片区负责人的真实姓名', max_length=20, blank=True, null=True)

    class Meta:
        db_table = 't_monthly_production_plan_items'
        verbose_name = '月度生产计划项目表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name + ' ' + self.specification